package com.bitters.one.repository;

import com.bitters.one.model.SysCarParts;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import javax.persistence.QueryHint;
import java.io.Serializable;
import java.util.List;

import static org.hibernate.jpa.QueryHints.HINT_COMMENT;

/**
 * description:
 * author: thomashe
 * date: 2022/3/9
 */
@Component
public interface SysCarPartsRepository extends JpaRepository<SysCarParts, Serializable> {

    @Query(value = " FROM SysCarParts s, SysCar p WHERE s.car_ids = p.id and p.name=?1")
    public List<SysCarParts> findByCarIdDQL(String name);

    @Query(nativeQuery = true, value = " select p.id,p.name,p.car_ids FROM sys_car_parts p,sys_car s  WHERE s.id = p.car_ids and s.name=?1")
    public List<SysCarParts> findByCarIdDQLSQL(String name);

    @QueryHints(value = {@QueryHint(name = HINT_COMMENT, value = "a query for pageable")})
    @Query("FROM SysCarParts s, SysCar p WHERE s.car_ids = p.id and p.name=:name")
    Page<SysCarParts> queryAllByPage(@Param("name") String name, Pageable pageable);
}
